package cn.edu.neusoft.demo.hospital2.Dao;

import cn.edu.neusoft.demo.hospital2.pojo.PatientDoctor;
import cn.edu.neusoft.demo.hospital2.pojo.dto.DepartmentStatsDTO;
import cn.edu.neusoft.demo.hospital2.pojo.dto.RegistrationDTO;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface PatientDoctorDao extends BaseMapper<PatientDoctor> {

    List<PatientDoctor> getAppointmentsByPatientId(String patientId);
    // 添加预约记录
    int addPatientDoctor(PatientDoctor patientDoctor);

    // 根据患者ID查询预约记录
    List<PatientDoctor> getPatientDoctorByPatientId(String patientId);

    // 取消预约
    int cancelAppointment(Integer id);

    // 根据ID查询预约记录
    PatientDoctor getAppointmentById(Integer id);

    // 更新预约记录
    int updateAppointment(PatientDoctor patientDoctor);

    // 查询所有预约记录
    List<PatientDoctor> getAllAppointments();

    @Select("<script>" +
            "SELECT pd.*, d.doc_name AS doctor_name, dept.department_name " +
            "FROM patient_doctor pd " +
            "LEFT JOIN doctor d ON pd.doc_id = d.doc_id " +
            "LEFT JOIN department dept ON pd.depart_id2 = dept.department_id " +
            "<where>" +
            "   <if test='startDate != null'> AND pd.day >= #{startDate} </if>" +
            "   <if test='endDate != null'> AND pd.day &lt;= #{endDate} </if>" +
            "   <if test='patientName != null and patientName != \"\"'> " +
            "       AND pd.name LIKE CONCAT('%', #{patientName}, '%') " +
            "   </if>" +
            "   <if test='doctorName != null and doctorName != \"\"'> " +
            "       AND d.doc_name LIKE CONCAT('%', #{doctorName}, '%') " +
            "   </if>" +
            "   <if test='departmentId != null'> AND pd.depart_id2 = #{departmentId} </if>" +
            "   <if test='status != null'> AND pd.status = #{status} </if>" +
            "</where>" +
            "ORDER BY pd.day DESC, pd.id DESC" +
            "</script>")
    List<RegistrationDTO> findRegistrations(
            @Param("startDate") String startDate,
            @Param("endDate") String endDate,
            @Param("patientName") String patientName,
            @Param("doctorName") String doctorName,
            @Param("departmentId") String departmentId,
            @Param("status") String status);

    @Update("UPDATE patient_doctor SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Integer id, @Param("status") String status);

    @Select("SELECT * FROM patient_doctor WHERE id = #{id}")
    PatientDoctor findById(Integer id);

    @Select("SELECT " +
            "  d.department_id, " +
            "  d.department_name, " +
            "  COUNT(pd.id) AS total_count, " +
            "  SUM(CASE WHEN pd.status = '1' THEN 1 ELSE 0 END) AS completed_count " +
            "FROM department d " +
            "LEFT JOIN patient_doctor pd ON d.department_id = pd.depart_id2 " +
            "GROUP BY d.department_id, d.department_name " +
            "ORDER BY total_count DESC")
    List<DepartmentStatsDTO> countByDepartment();
}